<?php

namespace App\Http\Requests\Tenant;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreTaskRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user() !== null;
    }

    public function rules(): array
    {
        return [
            'service_type' => [
                'required',
                'string',
                'max:100',
                Rule::exists('task_service_types', 'name'),
            ],
            'name' => 'required|string|max:32',
            'start_time' => 'required|date',
            'end_time' => 'required|date|after_or_equal:start_time',
            'settlement_cycle' => 'required|in:daily,weekly,monthly',
            'min_fee' => 'required|numeric|min:0',
            'max_fee' => 'required|numeric|min:0|gte:min_fee',
            'billing_unit' => 'required|string|max:20',
            'gender_requirement' => 'required|in:female,male,unlimited',
            'education_requirement' => 'required|in:unlimited,junior_high,high_school,college,bachelor,master,doctor',
            'min_age' => 'required|integer|min:16|max:100',
            'max_age' => 'required|integer|min:16|max:100|gte:min_age',
            'work_location' => 'required|string|max:200',
            'work_content' => 'required|string',
            'work_compensation' => 'required|string',
            'work_time' => 'required|string',
            'work_benefits' => 'nullable|string',
        ];
    }

    public function messages(): array
    {
        return [
            'service_type.required' => '请选择服务类型',
            'name.required' => '请输入任务名称',
            'name.max' => '任务名称最多32个字符',
            'start_time.required' => '请选择任务开始日期',
            'end_time.required' => '请选择任务结束日期',
            'end_time.after_or_equal' => '任务结束日期必须晚于开始日期',
            'settlement_cycle.in' => '请选择有效的结算周期',
            'billing_unit.required' => '请选择计费单位',
            'gender_requirement.in' => '请选择有效的性别要求',
            'education_requirement.in' => '请选择有效的学历要求',
            'max_fee.gte' => '最高计费需要大于或等于最低计费',
            'max_age.gte' => '最高年龄需要大于或等于最低年龄',
        ];
    }

    protected function prepareForValidation(): void
    {
        if ($this->has('name')) {
            $this->merge(['name' => trim($this->input('name'))]);
        }
    }
}
